package com.example.voluntary.controller;

import com.example.common.domain.PageResult;
import com.example.common.domain.Result;
import com.example.voluntary.domain.dto.ActivityAuditDTO;
import com.example.voluntary.domain.dto.ActivityQueryDTO;
import com.example.voluntary.domain.dto.VoluntarySearchDTO;
import com.example.voluntary.domain.dto.VolunteerActivitiesDTO;
import com.example.voluntary.domain.vo.*;
import com.example.voluntary.service.VolunteerActivitiesService;
import com.google.zxing.WriterException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@Tag(name = "志愿接口相关")
@Slf4j
@RestController
@RequestMapping("/voluntary")
@RequiredArgsConstructor
public class VoluntaryController {
    private final VolunteerActivitiesService voluntaryActivitiesService;

    /**
     * 志愿接口
     *
     * @param volunteerActivitiesDTO
     * @return
     */
    @PostMapping
    public Result voluntary(@RequestBody @Validated VolunteerActivitiesDTO volunteerActivitiesDTO) {
        log.info("创建志愿活动: {}", volunteerActivitiesDTO);
        voluntaryActivitiesService.createVoluntary(volunteerActivitiesDTO);
        return Result.success();
    }

    /**
     * 获取志愿活动列表
     *
     * @param page
     * @param pageSize
     * @return
     */
    @Operation(description = "查询活动列表")
    @GetMapping("/list")
    public Result<PageResult<VoluntaryVO>> getVoluntaryList(@RequestParam(defaultValue = "1") String page,
                                                            @RequestParam(defaultValue = "10") String pageSize) {

        Integer pageNum = Integer.valueOf(page);
        Integer size = Integer.valueOf(pageSize);
        log.info("获取志愿活动列表参数:页码:{},条数:{}", pageNum, size);
        PageResult<VoluntaryVO> voluntaryListVO = voluntaryActivitiesService.getVoluntaryList(pageNum, size);
        return Result.success(voluntaryListVO);
    }

    @Operation(description = "查询活动详细信息")
    @GetMapping("/detail/{activityId}")
    public Result<ActivityDetailVO> getActivityDetail(@PathVariable  @NotNull Integer activityId){
        log.info("查询活动详细信息:{}", activityId);
        ActivityDetailVO activityDetailVO = voluntaryActivitiesService.getActivityDetail(activityId);
        return Result.success(activityDetailVO);
    }

    @Operation(description = "添加参加记录")
    @PostMapping("/addParticipation")
    public Result addParticipation(@RequestParam("activityId") Integer activityId) {
        Integer id = Integer.valueOf(activityId);
        log.info("添加参加记录，活动id为: {}",id );
        voluntaryActivitiesService.addParticipation(id);
        return Result.success();
    }
    @Operation(description = "查询志愿者活动")
    @GetMapping("/search")
    public Result<PageResult<VoluntaryVO>> searchVolunteerActivity(VoluntarySearchDTO searchDTO) {

        log.info("查询志愿者活动参数:页码:{}",searchDTO);
        PageResult<VoluntaryVO> voluntaryVOPageResult = voluntaryActivitiesService.searchVolunteerActivity(searchDTO);
        return Result.success(voluntaryVOPageResult);
    }
    @Operation(description = "同步志愿活动数据到Elasticsearch")
    @PostMapping("/syncToElasticsearch")
    public Result syncToElasticsearch() {
        log.info("开始同步志愿活动数据到Elasticsearch");
        voluntaryActivitiesService.syncVolunteerActivitiesToElasticsearch();
        return Result.success("数据同步完成");
    }

    @Operation(description = "删除Elasticsearch中的数据")
    @DeleteMapping("/deleteElasticsearchData")
    public Result deleteElasticsearchData(String indexName) {
        log.info("开始删除Elasticsearch中的数据");
        voluntaryActivitiesService.deleteElasticsearchData(indexName);
        return Result.success("数据删除完成");
    }


    @Operation(description = "获取志愿活动管理列表")
    @GetMapping("/management/list")
    public Result<PageResult<VoluntaryManagementVO>> getVoluntaryManagementList(ActivityQueryDTO queryDTO) {
        log.info("获取志愿活动管理列表参数:{}", queryDTO);
        PageResult<VoluntaryManagementVO> voluntaryManagementVO = voluntaryActivitiesService.getVoluntaryManagementList(queryDTO);
        return Result.success(voluntaryManagementVO);
    }

    @Operation(description = "回显审核数据")
    @GetMapping("/management/audit/{activityId}")
    public Result<AuditVO> auditVoluntaryActivity(@PathVariable Integer activityId) {
        log.info("回显审核数据参数:{}", activityId);
        AuditVO auditVO = voluntaryActivitiesService.getAudit(activityId);
        return Result.success(auditVO);
    }


    @Operation(description = "审核志愿活动")
    @PostMapping("/management/audit")
    public Result auditVoluntaryActivity(@RequestBody ActivityAuditDTO auditDTO) throws IOException, WriterException {
        log.info("审核志愿活动参数:{}", auditDTO);
        voluntaryActivitiesService.auditVoluntaryActivity(auditDTO);
        return Result.success();
    }


    @Operation(description = "签到或签退")
    @PostMapping("/signInOrSignOut/activity")
    public Result signInOrSignOut(@RequestParam("activityId") Integer activityId) {
        log.info("签到或签退参数:{}", activityId);
        voluntaryActivitiesService.signInOrSignOut(activityId );
        return Result.success();
    }


    /**
     * 查询我申报的活动
     */
    @GetMapping("/my-created")
    public Result<List<CreatedActivityVO>> getMyCreatedActivities() {
        List<CreatedActivityVO> activities = voluntaryActivitiesService.getMyCreatedActivities();
        return Result.success(activities);
    }

    /**
     * 查询我参加的活动
     */
    @GetMapping("/my-participated")
    public Result<List<ParticipatedActivityVO>> getMyParticipatedActivities() {
        List<ParticipatedActivityVO> activities = voluntaryActivitiesService.getMyParticipatedActivities();
        return Result.success(activities);
    }



}
